package cn.darkhorse.mesadmin.controller.system;


import cn.darkhorse.messystem.pojo.MesSystemConfig;
import cn.darkhorse.messystem.service.MesSystemConfigService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 系统配置表 前端控制器
 * </p>
 *
 * @author maoguanjun
 * @since 2023-11-07
 */
@RestController
@RequestMapping("/system/mesSystemConfig")
public class MesSystemConfigController extends BaseController {
    @Resource
    private MesSystemConfigService mesSystemConfigService;
    @Resource
    private ISysConfigService sysConfigService;

    /**
     * 查看
     * @param keyWord
     * @return
     */
    @RequestMapping("/view")
    public AjaxResult view(String keyWord){
        MesSystemConfig config=mesSystemConfigService.view(keyWord);
        return AjaxResult.success(config);
    }

    /**
     * 保存
     * @param config
     * @return
     */
    @RequestMapping("/save")
    public AjaxResult save(@RequestBody MesSystemConfig config){
        boolean result=mesSystemConfigService.saveConfig(config);
        return AjaxResult.success(result);
    }

    @RequestMapping("/getRefreshInterval")
    public AjaxResult getRefreshInterval(){
        String val = sysConfigService.selectConfigByKey("sys.report.refreshInterval");
        if(null==val || val.isEmpty()){
            SysConfig config = new SysConfig();
            config.setConfigName("刷新时间间隔");
            config.setConfigKey("sys.report.refreshInterval");
            config.setConfigValue("60");
            config.setConfigType("N");
            sysConfigService.insertConfig(config);
            val = config.getConfigValue();
        }
        return AjaxResult.success("操作成功",val);
    }

    @RequestMapping("/setRefreshInterval")
    public AjaxResult setRefreshInterval(String val){
        SysConfig config = sysConfigService.selectSysConfigByKey("sys.report.refreshInterval");
        if (null==config){
            config = new SysConfig();
            config.setConfigName("刷新时间间隔");
            config.setConfigKey("sys.report.refreshInterval");
            config.setConfigType("N");
        }
        config.setConfigValue(val);
        sysConfigService.updateConfig(config);
        return AjaxResult.success();
    }

}

